<template>
	<view>
		<form @submit="formSubmit" class="register-form-container">
			<view class="top">
				<view class="avatar">
					<u-upload @afterRead="afterRead" name="1" :maxCount="1" :previewFullImage="false"
						class="uploadAvator">
						<u-avatar :src="avator" size="204" class="avatorimg" mode="widthFix"></u-avatar>
					</u-upload>
					<image src="/static/avator_icons/pix-small.png" class="slot-icon"></image>
				</view>
			</view>
			<view class="bottom">
				<view class="form-item">
					<view class="label">
						姓名：
					</view>
					<input class="form-input" name="userName" type="text" placeholder="请输入真实姓名" />
				</view>
				<view class="form-item">
					<view class="label">
						手机号码：
					</view>
					<input class="form-input" v-model="phone" type="number" placeholder="请输入手机号码" disabled />
					<!-- 	<button class="form-phone" open-type="getPhoneNumber"
						@getphonenumber="decryptPhoneNumber">{{phone_num}}</button> -->
				</view>
				<view class="form-item">
					<view class="label">
						密码：
					</view>
					<input class="form-input" name="password" type="password" placeholder="请输入密码" />
				</view>
				<view class="form-item">
					<view class="label">
						地产名称：
					</view>
					<input class="form-input" name="businessEstateName" placeholder="请输入地产名称" />
				</view>
				<view class="form-item">
					<view class="label">
						项目名称：
					</view>
					<input class="form-input" name="projectName" placeholder="请输入项目名称" />
				</view>
				<view class="form-item">
					<button class="form-btn" form-type="submit">完 成</button>
				</view>
			</view>

		</form>
	</view>
</template>

<script>
	import store from '@/store/store.js'
	import graceChecker from "@/common/graceChecker.js"
	export default {
		data() {
			return {
				phone: uni.getStorageSync('phone'),
				avator: '/static/avator_icons/avator-default.png',
				ISMOD: false,
				userInfoTemp:{}
			};
		},
		onLoad() {
			this.getStatusByWxId()
		},
		methods: {
			async getStatusByWxId() {
				const {
					data: res
				} = await uni.$http.get(uni.$http.baseURL + '/developer/getDeveloperInfoByWxId', {
					wxId: uni.getStorageSync('openId')
				})
				if (res.status == 0) {
					if (res.body == null) {
						this.ISMOD = false
					} else if (res.body.status != 2) {
						this.userInfoTemp=res.body
						//自动登录
						this.ISMOD = true
					}
				} else {
					uni.$showMsg(res.message)
				}
			},
			// 新增图片
			async afterRead(event) {
				// console.log(event)
				const result = await this.uploadFilePromise(event.file.url)
				const temp = JSON.parse(result)
				console.log(temp.body.fileId)
				store.dispatch('user/setAVATOR', temp.body.fileId)
				this.avator = uni.$http.baseURL + '/file/download?fileId=' + temp.body.fileId
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: uni.$http.baseURL + '/file/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							'fileType': 1
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						}
					});
				})
			},
			formSubmit: function(e) {
				let formData = e.detail.value
				const loginRule = [{
						name: "userName",
						checkType: "string",
						checkRule: "1,100",
						errorMsg: "真实姓名不能为空"
					},
					{
						name: "businessEstateName",
						checkType: "string",
						checkRule: "1,100",
						errorMsg: "地产名称不能为空"
					},
					{
						name: "projectName",
						checkType: "string",
						checkRule: "1,100",
						errorMsg: "项目名称不能为空"
					},
					{
						name: "password",
						checkType: "string",
						checkRule: "1,100",
						errorMsg: "密码不能为空"
					},
				]
				formData.phone = this.phone
				// if (formData.phone == '' || formData.phone == '请授权手机号') {
				// 	uni.$showMsg('手机号码不能为空')
				// 	return
				// }
				let checkRes = graceChecker.check(formData, loginRule)
				// formData.userType = '3'
				// if (!uni.getStorageSync('openId')) {
				// 	// this.getCode();
				// 	// formData.wxId = this.pathCode()
				// 	// formData.password = formData.wxId
				// } else {
				formData.wxId = uni.getStorageSync('openId')
				// formData.password = uni.getStorageSync('openId')

				// }
				//test
				// formData.phone = '18094711342'
				// formData.password = '123456'
				formData.headerUrl = uni.getStorageSync('avator')
				if (checkRes) {
					this.fecthData(formData)
				} else {
					uni.$showMsg(graceChecker.error)
				}
			},
			getCode() {
				uni.login({
					onlyAuthorize: true,
					success(e) {
						console.log('获取的code：' + e.code);
						store.dispatch('user/setCODE', e.code)
						// this.code = e.code
					}
				})
			},
			// pathCode() {
			// 	const code = store.getters['user/code']
			// 	return this.getOpenId(code)
			// },
			// async getOpenId(code) {
			// 	const {
			// 		data: res
			// 	} = await uni.$http.get(uni.$http.baseURL + '/user/getOpenId', {
			// 		code: code
			// 	})
			// 	if (res.status === 0) {
			// 		store.dispatch('user/setOpenID', res.body)
			// 		return res.body
			// 	} else {
			// 		this.pathCode()
			// 	}
			// },
			async fecthData(form) {
				if (this.ISMOD) {
					console.log(this.userInfoTemp)
					let _push_form =form
					_push_form.id=this.userInfoTemp.id
					// _push_form.updateTime=this.userInfoTemp.createTime
					_push_form.status=1
					const {
						data
					} = await uni.$http.post(uni.$http.baseURL + '/developer/updateDeveloperInfo', _push_form)
					console.log(data)
					if (data.status === 0) {
						uni.$showMsg('请等候审核，稍后尝试登录')
					} else {
						uni.$showMsg(data.message)
					}
				} else {
					const {
						data
					} = await uni.$http.post(uni.$http.baseURL + '/developer/register', form)
					console.log(data)
					if (data.status === 0) {
						uni.$showMsg('请等候审核，稍后尝试登录')
					} else {
						uni.$showMsg(data.message)
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.register-form-container {
		display: flex;
		flex-direction: column;

		.top {
			padding: 88rpx 273rpx 0 273rpx;

			.avatar {
				display: flex;
				justify-content: flex-end;
				align-items: flex-end;

				.avatorimg {
					width: 204rpx;
					height: 204rpx;
				}

				.slot-icon {
					height: 50rpx;
					width: 50rpx;
					margin-right: 7rpx;
					margin-bottom: 22rpx;
					position: absolute;
				}
			}
		}

		.bottom {
			padding: 24rpx 95rpx 0 95rpx;
		}
	}

	.form-item {
		display: flex;
		flex-direction: column;
		width: 560rpx;
		margin-top: 20rpx;

		.form-input {
			padding-left: 60rpx;
			margin-top: 30rpx;
			height: 88rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
			border-radius: 44rpx;
		}

		.form-phone {
			color: #999999;
			text-align: left;
			padding-left: 60rpx;
			margin-top: 30rpx;
			width: 560rpx;
			height: 88rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
			border-radius: 44rpx;
		}

		.form-btn {
			color: #FFFFFF;
			height: 88rpx;
			width: 100%;
			margin-top: 120rpx;
			background: linear-gradient(90deg, #E62129 0%, #BA000A 100%);
			box-shadow: 0rpx 5rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);
			border-radius: 44rpx;
			margin-bottom: 293rpx;
		}
	}

	.label {
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
	}
</style>
